<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="my-card">
        <div slot="header" class="clearfix">
          <!-- 左侧表单 -->
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item>
              <el-input
                v-model.trim="query.title"
                placeholder="根据用户名搜索"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" class="my-shou" @click="query.title=''"
                >清除</el-button
              >
              <el-button type="primary" class="my-cha" @click="setonSubmit"
                >查询</el-button
              >
            </el-form-item>
            <el-form-item style="float: right">
              <!-- 右侧 -->
              <el-button class="my-btn" style="padding: 3px 0" type="text"
              @click="$refs.addPerm.dialogVisible = true">
                <i class="el-icon-edit"></i>
                新增权限组
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-alert type="info" class="el-icon-info" :title="'共'+total+'条消息'"></el-alert>

        <!-- 列表 -->
        <el-table :data="list" ref="multipleTable" style="width: 100%"
        tooltip-effect="dark" @selection-change="handleSelectionChange">
          
          <el-table-column
            type="selection"
            width="55"
            v-model="list.id">
          </el-table-column>
          <el-table-column prop="title" label="用户名" width="300" align="center">
          </el-table-column>
          <el-table-column label="日期" sortable width="500" align="center">
            <template slot-scope="{row}">
              {{row.update_date|formatDate}}
            </template>
          </el-table-column>
          <el-table-column  label="操作"  id="test-btn" align="center">
            <template slot-scope="{ row }">
              <el-button type="primary" icon="el-icon-edit" circle plain @click="datapermissions(row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" circle plain @click="delpermissions(row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="block" style="float: right; margin: 20px">
          <!-- <span class="demonstration">完整功能</span> -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[2, 4, 6, 8]"
            :page-size="query.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-card>
      <addPermissions ref="addPerm" @update-list="getpermissions"></addPermissions>
      <dataPermissions ref="dataPerm" @update-list="getpermissions"></dataPermissions>
    </div>
    
  </div>
</template>

<script>
import {getpermissions,delpermissions,getperm} from '@/api/permissions'
import addPermissions from '@/views/base/components/add-permissions'
import dataPermissions from '@/views/base/components/data-permissions'

export default {
  components:{addPermissions,dataPermissions},
  name: "permissions",
  data() {
    return {
      check:'',
      checkAll: false,
      isIndeterminate: true,
      dialogVisible: false,
      total:'',
      query: {
        page: 1,
        pagesize: 2,
        title:""
      },
      list:[],
      formInline: {
        user: "",
        region: "",
      },

    };
  },
  methods: {
    setonSubmit(){
      this.getpermissions()
    },
      handleSizeChange(newPagesize) {
        this.query.pagesize = newPagesize
        this.getpermissions()
      },
      handleCurrentChange(newPage) {
        console.log(newPage);
        this.query.page = newPage
        this.getpermissions()
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.check = val;
      },
    async getpermissions() {
      let res = await getpermissions(this.query)
      console.log(123);
      this.list=res.list
      this.total=res.counts
      console.log(res);
    },
    // handleClose(done) {
    //   this.$confirm('确认关闭？')
    //     .then(_ => {
    //       done();
    //     })
    //     .catch(_ => {});
    // }
    async delpermissions(res){
      console.log(res);
      await delpermissions(res)
              // 3. 成功提示，失败处理
      this.$message.success('删除成功')
      this.getpermissions()
    },
    datapermissions(res){
      console.log(123);
      console.log(res);
      this.$refs.dataPerm.isShow = true;
      this.getperm(res)
    },
    async getperm(res){
      const data=await getperm(res)
      this.$refs.dataPerm.dataForm(data)
    }
  },
  created(){
    this.getpermissions()
  }
};
</script>

<style lang="scss" scoped>
// [data-v-f70f99cc]
// [data-v-f70f99cc]
// my-card
.my-card[data-v-f70f99cc] .el-card__header {
  padding: 100px;
  border-bottom: 5px solid #ebeef5 !important;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
</style>
<style>
.my-card .el-card__header {
  padding-bottom: 0px;
  border-bottom: 0px solid #ebeef5 !important;
}
.my-card .el-card__body {
  padding-top: 0px;
}
.my-card .el-form-item {
  margin: 10px 0;
}
.my-card .el-alert {
  margin: 8px 0 16px;
}
.my-card .el-table__header-wrapper {
  border-bottom: 2px solid #e8e8e8;
  background-color: #fafafa;
}
.my-card th {
  background-color: #fafafa !important;
}
.my-card .my-btn {
  width: 95px;
  height: 32px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
}
.my-card .my-shou {
  width: 56px;
  height: 32px;
  padding: 0;
  line-height: 0;
  margin-left: 10px;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
}
.my-card .my-cha {
  width: 56px;
  height: 32px;
  padding: 0;
  line-height: 0;
}
.my-card .el-input__inner {
  height: 32px;
}
.my-card .my-item-two {
  margin-left: 40px;
}


</style>